//
//  THFlowEarnMyPublishVC.m
//  YJB
//
//  Created by 中电大维 on 2017/1/12.
//  Copyright © 2017年 YJB. All rights reserved.
//  我发布的

#import "THFlowEarnMyPublishVC.h"
#import "THFlowEarnMypublishModel.h"
#import "THFlowEarnMypublishCell.h"
#import "THFlowEarnMyJoinVC.h"
#import "THFlowEarnMyPubDetailVC.h"
#import "THFlowEarnAutoMyPubModel.h"

@interface THFlowEarnMyPublishVC ()<FlowEarnMypublishDelegate>

@end

@implementation THFlowEarnMyPublishVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.navTitle.length) {
        self.title = self.navTitle;
    }
    self.tableView.tableFooterView = [UIView new];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView registerClass:[THFlowEarnMypublishCell class] forCellReuseIdentifier:flowEarnMypublishCellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView setSeparatorColor:[UIColor colorWithHexString:@"e5e5e5"]];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
    [NOTIFICATIONCENTER addObserver:self selector:@selector(loadData) name:THFlowEarnPublishSuccessNotification object:nil];
}

- (void)loadData
{
#pragma mark - 全部的列表
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *url = [[NSString alloc] init];
    NSString *userName = [YJUserDefaultTool readUserName];
    NSString *md5Key = [YJUserDefaultTool readKeyID];
    
    if (self.isAuto) {
        params[@"action"] = @"get_surf_list";
        url = THURL(@"traffic.php");
    }else{
        params[@"action"] = @"get_mypublish_list";
        url = THURL(@"manual.php");
    }
    params[@"makegold"] = @{@"username":userName,
                            @"firstrow":@"-1",
                            @"listrows":@"",
                            @"user_info":userName,
                            @"phone_info":@"iOS",
                            @"md5_key":md5Key};
    [YJNetworkTool postWithUrl:url parameters:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        NSString *code = responseObject[@"return"][@"code"];
        if (code.integerValue == 200)
        {
            NSArray *array = responseObject[@"return"][@"data"][@"list"];
            if (self.isAuto) {
                self.tasks = [THFlowEarnAutoMyPubModel mj_objectArrayWithKeyValuesArray:array];
            }else{
                self.tasks = [THFlowEarnMypublishModel mj_objectArrayWithKeyValuesArray:array];
            }
        }
        else if(code.integerValue == 500)
            [self.tasks removeAllObjects];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
        [self.tableView.mj_header endRefreshing];
    }];
}


- (void)detailItem:(UIButton *)button
{
    THFlowEarnMypublishCell *cell = button.superview;
    MyLog(@"THFlowEarnMypublishCell detailItem tag:%ld",(long)cell.tag);
    THFlowEarnMyJoinVC *vc = [[THFlowEarnMyJoinVC alloc] init];
    vc.navTitle = @"完成详情";
    THFlowEarnMypublishModel *model = self.tasks[cell.tag];
    vc.taskID = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tasks.count;
    //return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THFlowEarnMypublishCell *cell = [tableView dequeueReusableCellWithIdentifier:flowEarnMypublishCellID forIndexPath:indexPath];
    cell.delegate = self;
    
    if (self.isAuto) {
        cell.vipAwardBtn.hidden = YES;
        cell.autoModel = self.tasks[indexPath.row];
    }else{
        cell.model = self.tasks[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tag = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    THFlowEarnMypublishModel *model = self.tasks[indexPath.row];
    THFlowEarnMyPubDetailVC *vc = [[THFlowEarnMyPubDetailVC alloc] init];
    vc.isAuto = self.isAuto;
    vc.taskId = model.ID;
    vc.taskIcon = model.url;
    vc.surfedcount = model.surfedcount;
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THFlowEarnMypublishModel *model = self.tasks[indexPath.row];
    return model.cellHeight;
    //return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}


@end
